package cn.byapps.sell.admin.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import cn.byapps.sell.dto.OrderDTO;
import cn.byapps.sell.enums.ResultEnum;
import cn.byapps.sell.exception.SellException;
import cn.byapps.sell.service.OrderService;
import lombok.extern.slf4j.Slf4j;

@Controller
@RequestMapping("/seller/order")
@Slf4j
public class SellerOrderController {

	@Autowired
	private OrderService orderService;

	/**
	 * 订单列表
	 * 
	 * @param page 第几页, 从1页开始
	 * @param size 一页有多少条数据
	 * @return
	 */
	@GetMapping("/list")
	public String list(@RequestParam(value = "page", defaultValue = "1") Integer page,
			@RequestParam(value = "size", defaultValue = "10") Integer size, Map<String, Object> map) {
		PageRequest request = PageRequest.of(page - 1, size);
		Page<OrderDTO> orderDTOPage = orderService.findList(request);
		map.put("orderDTOPage", orderDTOPage);
		map.put("currentPage", page);
		map.put("size", size);
//        orderDTOPage.getTotalPages()
		return "order/list";
	}

	/**
	 * 取消订单
	 * 
	 * @param orderId
	 * @return
	 */
	@GetMapping("/cancel")
	public String cancel(@RequestParam("orderId") String orderId, Map<String, Object> map) {
		try {
			OrderDTO orderDTO = orderService.findOne(orderId);
			orderService.cancel(orderDTO);
		} catch (SellException e) {
			log.error("【卖家端取消订单】发生异常{}", e);
			map.put("msg", e.getMessage());
			map.put("url", "/sell/seller/order/list");
			return "common/error";
		}

		map.put("msg", ResultEnum.ORDER_CANCEL_SUCCESS.getMessage());
		map.put("url", "/sell/seller/order/list");
		return "common/success";
	}

	/**
	 * 订单详情
	 * 
	 * @param orderId
	 * @param map
	 * @return
	 */
	@GetMapping("/detail")
	public String detail(@RequestParam("orderId") String orderId, Map<String, Object> map) {
		OrderDTO orderDTO = new OrderDTO();
		try {
			orderDTO = orderService.findOne(orderId);
		} catch (SellException e) {
			log.error("【卖家端查询订单详情】发生异常{}", e);
			map.put("msg", e.getMessage());
			map.put("url", "/sell/seller/order/list");
			return "common/error";
		}

		map.put("orderDTO", orderDTO);
		return "order/detail";
	}

	/**
	 * 完结订单
	 * 
	 * @param orderId
	 * @param map
	 * @return
	 */
	@GetMapping("/finish")
	public String finished(@RequestParam("orderId") String orderId, Map<String, Object> map) {
		try {
			OrderDTO orderDTO = orderService.findOne(orderId);
			orderService.finish(orderDTO);
		} catch (SellException e) {
			log.error("【卖家端完结订单】发生异常{}", e);
			map.put("msg", e.getMessage());
			map.put("url", "/sell/seller/order/list");
			return "common/error";
		}

		map.put("msg", ResultEnum.ORDER_FINISH_SUCCESS.getMessage());
		map.put("url", "/sell/seller/order/list");
		return "common/success";
	}
}
